package cn.uniquesoft.api;

import cn.uniquesoft.service.IBankService;
import cn.uniquesoft.service.ICompanyService;
import cn.uniquesoft.service.IConnectService;
import cn.uniquesoft.service.IConsultantService;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.CompanyVO;
import cn.uniquesoft.vo.ConnectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/connect")
public class ApiConnectController {
    @Autowired
    private IConnectService connectService;
    @Autowired
    private IConsultantService consultantService;
    @Value("${resource.pagesize}")
    private int pagesize;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IBankService bankService;
    @RequestMapping(value = "/insertConnect")
    @ResponseBody
    public AnswerVO insertConnect(ConnectVO item) {
        //int icompany,String ccompany,int icompanyuser,String ccompanyusern,int ibank,String cbank,int
        int icompany = item.getIcompany();
        CompanyVO c = this.companyService.findOneById(icompany);
        item.setCshortname(c.getCshort());
        System.out.println("------------------------"+c.getCshort());
        AnswerVO answerVO = new AnswerVO();
        // 通过金融机构id和企业id查看是否有历史记录，如果有则判断是否修改，如果没有则新增
        List<ConnectVO> list = this.connectService.findByTwoId(item);
        if(list.size()==0){
            item.setIscompanyconnect(1);
            item.setIsbankconnect(2);
           int i =  this.connectService.save(item);
           if(i>0){
               answerVO.setSuccess(true);
               answerVO.setMsg("提交成功");
           }
        }else{
            if(list.get(0).getIscompanyconnect() == 2 && list.get(0).getIconsultant() == item.getIconsultant()){
                //修改 企业是否对接状态形成银企对接
                ConnectVO  newItem = list.get(0);
                newItem.setIscompanyconnect(1);
                newItem.setCcompanyuser(item.getCcompanyuser());
                newItem.setIcompanyuser(item.getIcompanyuser());
                newItem.setDconnecttime(new Date());
                this.connectService.edit(newItem);
                answerVO.setSuccess(true);
                answerVO.setMsg("提交成功");
            }else{
                answerVO.setSuccess(false);
                answerVO.setMsg("银企对接已存在！");
            }
        }
        return answerVO;
    }


    @RequestMapping(value = "/insertConnectBybank")
    @ResponseBody
    public AnswerVO insertConnectBybank(ConnectVO item) {
        AnswerVO answerVO = new AnswerVO();
        // 通过金融机构id和企业id查看是否有历史记录，如果有则判断是否修改，如果没有则新增
        int icompany = item.getIcompany();
        CompanyVO c = this.companyService.findOneById(icompany);
        item.setCshortname(c.getCshort());
        System.out.println("------------------------"+c.getCshort());
        List<ConnectVO> list = this.connectService.findByTwoId(item);

        if(list.size()==0){
            item.setIscompanyconnect(2);
            item.setIsbankconnect(1);
            item.setCconsultant(this.consultantService.findOneById(item.getIconsultant()).getCname());
            int i =  this.connectService.save(item);
            if(i>0){
                answerVO.setSuccess(true);
                answerVO.setMsg("提交成功");
            }
        }else{
            if(list.get(0).getIsbankconnect() == 2 && list.get(0).getIconsultant() == item.getIconsultant()){
                //修改 企业是否对接状态形成银企对接
                ConnectVO  newItem = list.get(0);
                newItem.setIscompanyconnect(1);
                newItem.setDconnecttime(new Date());
                this.connectService.edit(newItem);
                answerVO.setSuccess(true);
                answerVO.setMsg("提交成功");
            }else{
                answerVO.setSuccess(false);
                answerVO.setMsg("银企对接已存在！");
            }
        }
        return answerVO;
    }


    @RequestMapping(value = "/getConnect")
    @ResponseBody
    public  List<ConnectVO>  getConnect(int iid, int iorgname){

        List<ConnectVO> itemList = new ArrayList<>();
        ConnectVO item = new ConnectVO();
        item.setIid(iid);
        item.setPageSize(this.pagesize);
        if(iorgname == 0){

        }else{
            item.setIbank(iorgname);
        }
        itemList = this.connectService.getConnectList(item);
        for(ConnectVO c : itemList){
            c.setCcpmpanypic(this.companyService.findOneById(c.getIcompany()).getCpicpath());
            c.setCbankpic(this.bankService.findOneById(c.getIbank()).getCpicpath());
        }
        return itemList;

    }


    @RequestMapping(value = "/toSaveConnectDetail")
    @ResponseBody
    public  AnswerVO  toSaveConnectDetail(ConnectVO item) {
        AnswerVO answerVO = new AnswerVO();
        item.setIsbankconnect(1);
        item.setDconnecttime(new Date());
        int i = this.connectService.updateDeatils(item);
        if (i>0){
            answerVO.setSuccess(true);
            answerVO.setMsg("保存成功");
        }else{
            answerVO.setSuccess(false);
            answerVO.setMsg("保存失败");
        }

        return  answerVO;
    }

    @RequestMapping(value = "/updateConnect")
    @ResponseBody
    public AnswerVO updateConnect(int iid, int itype) {
        AnswerVO answerVO = new AnswerVO();
        ConnectVO c = new ConnectVO();
        int i = 0;
        if (itype == 1) {
            c.setIid(iid);
            c.setIscompanyconnect(1);
            c.setDconnecttime(new Date());
            i = this.connectService.updateConnectBycompanyUser(c);
        } else {
            c.setIid(iid);
            c.setIsbankconnect(1);
            c.setDconnecttime(new Date());
            i = this.connectService.updateConnectBybankUser(c);
        }
        if (i > 0) {
            answerVO.setSuccess(true);
            answerVO.setMsg("保存成功");
        } else {
            answerVO.setSuccess(false);
            answerVO.setMsg("保存失败");
        }

        return answerVO;
    }

    @RequestMapping(value = "/getConnectCount")
    @ResponseBody
    public AnswerVO getConnectCount() {
        AnswerVO answerVO = new AnswerVO();
        List<Integer> l = new ArrayList<>();
        List<ConnectVO> vlist =this.connectService.findAll(null);
        int i = 0;
        int j = 0;
        for(ConnectVO v: vlist){
            if(v.getIsbankconnect() ==1 && v.getIscompanyconnect() ==1){
                i++;
            }else{
                j++;
            }
        }
        l.add(i);
        l.add(j);
        l.add(vlist.size());
        answerVO.setData(l);
        return answerVO;
    }
}
